﻿using ControllerLayer;
using ModelLayer;
using System;
using WcfBetterPlace.WcfData;

namespace WcfBetterPlace
{
    public class BetterAdminService : IBetterAdminService
    {
        #region Customer

        public IntData CreateCustomer(Customer customer)
        {
            PersonCtr personCtr = new PersonCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = personCtr.CreateCustomer(customer);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not create the customer.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public PersonData GetCustomer(long id)
        {
            PersonCtr personCtr = new PersonCtr();
            PersonData serviceData = new PersonData();
            try
            {
                serviceData.SerData = personCtr.GetCustomer(id);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the customer.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData UpdateCustomer(Customer customer)
        {
            PersonCtr personCtr = new PersonCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = personCtr.UpdateCustomer(customer);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not update the customer.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData DeleteCustomer(long customerId)
        {
            PersonCtr personCtr = new PersonCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = personCtr.DeleteCustomer(customerId);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not delete the customer.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public PersonList GetAllCustomers()
        {
            PersonCtr personCtr = new PersonCtr();
            PersonList serviceData = new PersonList();
            try
            {
                serviceData.SerData = personCtr.GetAllCustomer();
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the customers.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        #endregion Customer

        #region Reservation

        public ReservationData CreateReservationUsingDijkstras(long startStationId, long endStationId, long customerId, DateTime date)
        {
            BetterClientService clientService = new BetterClientService();
            return clientService.CreateReservationUsingDijkstras(startStationId, endStationId, customerId, date);
        }

        public ReservationData CreateReservationUsingAStar(long startStationId, long endStationId, long customerId, DateTime date)
        {
            BetterClientService clientService = new BetterClientService();
            return clientService.CreateReservationUsingDijkstras(startStationId, endStationId, customerId, date);
        }

        public ReservationData GetReservation(long id)
        {
            BetterClientService clientService = new BetterClientService();
            return clientService.GetReservation(id);
        }

        public ReservationList GetAllReservations()
        {
            ReservationCtr reservationCtr = new ReservationCtr();
            ReservationList serviceData = new ReservationList();
            try
            {
                serviceData.SerData = reservationCtr.GetAllReservations();
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get all reservations.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData UpdateReservation(Reservation resv)
        {
            BetterClientService clientService = new BetterClientService();
            return clientService.UpdateReservation(resv);
        }

        public IntData CancelReservation(long resvId)
        {
            BetterClientService clientService = new BetterClientService();
            return clientService.CancelReservation(resvId);
        }

        #endregion Reservation

        #region Station

        public StationList GetAllStations()
        {
            StationCtr stationCtr = new StationCtr();
            StationList stationList = new StationList();
            try
            {
                stationList.SerData = stationCtr.GetAllStation();
            }
            catch (Exception ex)
            {
                stationList.SerStatus = "Error: Could not get all stations";
                stationList.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return stationList;
        }

        public void AddBattery(long stationId)
        {
            StationCtr stationCtr = new StationCtr();
            stationCtr.AddBattery(stationId);
        }

        public bool RemoveBattery(long stationId)
        {
            StationCtr stationCtr = new StationCtr();
            return stationCtr.RemoveBattery(stationId);
        }

        public IntData GetBatteries(long stationId)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.GetBatteries(stationId);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the batteries from the station.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public void SetBatteries(long stationId, int number)
        {
            StationCtr stationCtr = new StationCtr();
            stationCtr.SetBatteries(stationId, number);
        }

        public IntData CreateStation(Station station)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.CreateStation(station);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not create the station.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public StationData GetStation(long id)
        {
            StationCtr stationCtr = new StationCtr();
            StationData serviceData = new StationData();
            try
            {
                serviceData.SerStatus = "Ok";
                serviceData.SerData = stationCtr.GetStation(id);
                serviceData.SerErrorDescription = "";
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the station.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData UpdateStation(Station station)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.UpdateStation(station);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not update the station.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData DeleteStation(long stationId)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.DeleteStation(stationId);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not delete the station.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        #endregion Station

        #region Edge

        public EdgeList GetAllEdges()
        {
            StationCtr stationCtr = new StationCtr();
            EdgeList serviceData = new EdgeList();
            try
            {
                serviceData.SerData = stationCtr.GetAllEdges();
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get all edges.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public EdgeList GetStationEdges(long id)
        {
            StationCtr stationCtr = new StationCtr();
            EdgeList serviceData = new EdgeList();
            try
            {
                serviceData.SerData = stationCtr.GetStationEdges(id);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get station edges.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData CreateEdge(Edge edge)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.CreateEdge(edge);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not create the edge.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public IntData DeleteEdge(long station1, long station2)
        {
            StationCtr stationCtr = new StationCtr();
            IntData serviceData = new IntData();
            try
            {
                serviceData.SerData = stationCtr.DeleteEdge(station1, station2);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not delete the edge.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        #endregion Edge

        #region City

        public CityData GetCityFromPostalCode(int postalCode)
        {
            CityCtr cityCtr = new CityCtr();
            CityData serviceData = new CityData();
            try
            {
                serviceData.SerData = cityCtr.GetCityFromPostalCode(postalCode);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the city from postalcode.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        public CityData GetCityFromName(string cityName)
        {
            CityCtr cityCtr = new CityCtr();
            CityData serviceData = new CityData();
            try
            {
                serviceData.SerData = cityCtr.GetCityFromName(cityName);
            }
            catch (Exception ex)
            {
                serviceData.SerStatus = "Error: Could not get the city from city name.";
                serviceData.SerErrorDescription = "Error details:\n" + ex.Message;
            }
            return serviceData;
        }

        #endregion City
    }
}